package com.yanyi.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.yanyi.music.domain.SongList;
import com.yanyi.music.service.impl.SongListServiceImpl;
import com.yanyi.music.utils.Consts;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 歌单控制类
 */
@Api(tags = "歌单API")
@RestController
@RequestMapping("/songList")
public class SongListController {
    @Autowired
    private SongListServiceImpl songListService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addSongList(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String title = request.getParameter("title");
        String pic = request.getParameter("pic");
        String introduction = request.getParameter("introduction");
        String style = request.getParameter("style");
        SongList songList = new SongList();
        songList.setTitle(title);
        songList.setPic(pic);
        songList.setStyle(style);
        songList.setIntroduction(introduction);
        int insert = songListService.insert(songList);
        if (insert == 1) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "添加成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        jsonObject.put(Consts.MSG, "添加失败");
        return jsonObject;
    }

    /**
     * 修改歌单
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateSongList(HttpServletRequest request) {
        JSONObject object = new JSONObject();
        String title = request.getParameter("title");
        String introduction = request.getParameter("introduction");
        String style = request.getParameter("style");
        SongList songList = new SongList();
        songList.setTitle(title);
        songList.setStyle(style);
        songList.setIntroduction(introduction);
        int flag = songListService.updateByPrimaryKeySelective(songList);
        if (flag == 1) {
            object.put(Consts.CODE, 1);
            object.put(Consts.MSG, "修改成功");
            return object;
        }
        object.put(Consts.CODE, 0);
        object.put(Consts.MSG, "修改失败");
        return object;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Object delSongList(HttpServletRequest request) {
        return songListService.deleteByPrimaryKey(Integer.valueOf(request.getParameter("id")));
    }

    /**
     * 查询所有歌单
     */
    @RequestMapping(value = "/allSongList", method = RequestMethod.GET)
    public Object allSongList(HttpServletRequest request) {
        return songListService.findAll(null);
    }

    /**
     * 根据标题模糊查询歌单列表
     */
    @RequestMapping(value = "/likeTitle", method = RequestMethod.GET)
    public Object likeTitle(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        //歌单标题
        map.put("likeTitle", "%" + request.getParameter("title").trim() + "%");
        return songListService.findAll(map);
    }

    /**
     * 根据风格模糊查询歌单列表
     */
    @RequestMapping(value = "/likeStyle", method = RequestMethod.GET)
    public Object likeStyle(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        //歌单风格
        map.put("style", "%" + request.getParameter("style").trim() + "%");
        return songListService.findAll(map);
    }
    /**
     * 更新歌单图片
     */
    @RequestMapping(value = "/updateSongListPic",method = RequestMethod.POST)
    public Object updateSongListPic(@RequestParam("file") MultipartFile avatarFile, @RequestParam("id")int id){
        JSONObject jsonObject = new JSONObject();
        if(avatarFile.isEmpty()){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"文件上传失败");
            return jsonObject;
        }
        //文件名=当前时间到毫秒+原来的文件名
        String fileName = System.currentTimeMillis()+avatarFile.getOriginalFilename();
        //文件路径
        String filePath = System.getProperty("user.dir")+System.getProperty("file.separator")+"img"
                +System.getProperty("file.separator")+"songListPic";
        //如果文件路径不存在，新增该路径
        File file1 = new File(filePath);
        if(!file1.exists()){
            file1.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath+System.getProperty("file.separator")+fileName);
        //存储到数据库里的相对文件地址
        String storeAvatorPath = "/img/songListPic/"+fileName;
        try {
            avatarFile.transferTo(dest);
            SongList songList = new SongList();
            songList.setId(id);
            songList.setPic(storeAvatorPath);
            int flag = songListService.updateByPrimaryKeySelective(songList);
            if(flag==1){
                jsonObject.put(Consts.CODE,1);
                jsonObject.put(Consts.MSG,"上传成功");
                jsonObject.put("pic",storeAvatorPath);
                return jsonObject;
            }
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"上传失败");
            return jsonObject;
        } catch (IOException e) {
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"上传失败"+e.getMessage());
        }finally {
            return jsonObject;
        }
    }
}
